﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChilishopDataLibrary
{
    public sealed class Data
    {
        public static List<Category> GetCategories()
        {
            return Category.GetAllCategories();
        }

        public static List<Product> GetProducts()
        {
            return Product.GetAllProducts();
        }
    }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public Category(int p1, string p2)
        {
            // TODO: Complete member initialization
            this.Id = p1;
            this.Name = p2;
        }

        public static List<Category> GetAllCategories()
        {
            List<Category> rez = new List<Category>();
            string filename=@"D:\dolog\itthon\ChiliShop\WebApplication1\LIBRARY\ChilishopData\ChilishopDataLibrary\resource\categories.csv";
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                StreamReader sr = new StreamReader(fs);
                string str = sr.ReadLine();
                //string s = Console.ReadLine();
                while (str != null)
                {
                    string[] xu = new String[2];
                    xu = str.Split(',');
                    rez.Add(new Category(int.Parse(xu[0]), xu[1]));
                    str = sr.ReadLine();
                } sr.Close();
            }
            return rez;
        }
    }

    public class Product
    {
        public Product(int id, string name, string description, decimal price, int catId,
            string imagePath, bool isNew = false)
        {
            this.Id = id;
            this.Name = name;
            this.Description = description;
            this.Price = price;
            this.CategoryId = catId;
            this.IsNew = isNew;
        }
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public int CategoryId { get; set; }
        public string ImagePath { get; set; }
        public bool IsNew { get; set; }

        internal static List<Product> GetAllProducts()
        {
            List<Product> rez = new List<Product>();
            string filename = @"D:\dolog\itthon\ChiliShop\WebApplication1\LIBRARY\ChilishopData\ChilishopDataLibrary\resource\products.csv";
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(936));
                string str = sr.ReadLine();
                //string s = Console.ReadLine();
                while (str != null)
                {                    
                    string[] xu = new String[6];
                    xu = str.Split(',');
                    rez.Add(new Product(int.Parse(xu[0]),xu[1],xu[2],decimal.Parse(xu[3]),int.Parse(xu[4]),xu[5]));
                    str = sr.ReadLine();
                } sr.Close();
            }
            return rez;
    
        }
    }
}
